package com.czy.moudle.exception.domain;

import com.czy.moudle.exception.domain.enums.Level;
import com.czy.moudle.exception.domain.enums.Type;
import org.jsoup.Connection;

/***
 * 自定义异常基类
 *
 * @author 陈致远
 * @date 2016/8/11
 */
public class BaseExceptation extends RuntimeException {
    /**
     * 对应ExceptionConfig中的errKey
     */
    private String errKey;

    /**
     * 对应ExceptionConfig中的errCode
     */
    private Long errCode;

    /**
     * 类型
     */
    private Level level;

    /**
     * 类型：默认为未定义异常
     */
    private Type type;

    /**
     * 说明
     */
    private String content;

    /**
     * 私有构造方法
     */
    private BaseExceptation() {
    }

    public static BaseExceptation buildException(String errKey, Long errCode, Level level, Type type) {
        BaseExceptation exceptation = new BaseExceptation();
        exceptation.setErrKey(errKey);
        exceptation.setErrCode(errCode);
        exceptation.setLevel(level);
        exceptation.setType(type);

        return exceptation;
    }

    public static BaseExceptation buildWebException(String content) {
        BaseExceptation webException = BaseExceptation.buildException("com.czy.exception.web", 20001L, Level.info, Type.web);
        webException.setContent(content);
        return webException;
    }

    public static BaseExceptation buildDataBaseException(String content) {
        BaseExceptation dataException = BaseExceptation.buildException("com.czy.exception.database", 20002L, Level.info, Type.database);
        dataException.setContent(content);
        return dataException;
    }

    @Override
    public String toString() {
        return "errorKey: " + errKey +
                ",异常编码： " + errCode +
                ", 异常等级: " + level +
                ", 异常类型： " + type +
                ", 异常内容： " + content;
    }

    public String getErrKey() {
        return errKey;
    }

    public void setErrKey(String errKey) {
        this.errKey = errKey;
    }

    public Long getErrCode() {
        return errCode;
    }

    public void setErrCode(Long errCode) {
        this.errCode = errCode;
    }

    public Level getLevel() {
        return level;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }
}
